home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / GLUT / progs / examples / glutdino.c < prev    next >
C/C++ Source or Header  |  1996-11-11  |  8KB  |  248 lines

  1.  
  2. /* Copyright (c) Mark J. Kilgard, 1994.  */
  3.  
  4. /* This program is freely distributable without licensing fees 
  5.    and is provided without guarantee or warrantee expressed or 
  6.    implied. This program is -not- in the public domain. */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <math.h>       /* for cos(), sin(), and sqrt() */
  12. #include <GL/glu.h>
  13. #include <GL/glut.h>
  14.  
  15. typedef enum {
  16.   RESERVED, BODY_SIDE, BODY_EDGE, BODY_WHOLE, ARM_SIDE, ARM_EDGE, ARM_WHOLE,
  17.   LEG_SIDE, LEG_EDGE, LEG_WHOLE, EYE_SIDE, EYE_EDGE, EYE_WHOLE, DINOSAUR
  18. } displayLists;
  19.  
  20. GLfloat angle = -150;   /* in degrees */
  21. GLboolean doubleBuffer = GL_TRUE, iconic = GL_FALSE, keepAspect = GL_FALSE;
  22. int moving, begin;
  23. int W = 300, H = 300;
  24. GLdouble bodyWidth = 2.0;
  25. int newModel = 1;
  26. /* *INDENT-OFF* */
  27. GLfloat body[][2] = { {0, 3}, {1, 1}, {5, 1}, {8, 4}, {10, 4}, {11, 5},
  28.   {11, 11.5}, {13, 12}, {13, 13}, {10, 13.5}, {13, 14}, {13, 15}, {11, 16},
  29.   {8, 16}, {7, 15}, {7, 13}, {8, 12}, {7, 11}, {6, 6}, {4, 3}, {3, 2},
  30.   {1, 2} };
  31. GLfloat arm[][2] = { {8, 10}, {9, 9}, {10, 9}, {13, 8}, {14, 9}, {16, 9},
  32.   {15, 9.5}, {16, 10}, {15, 10}, {15.5, 11}, {14.5, 10}, {14, 11}, {14, 10},
  33.   {13, 9}, {11, 11}, {9, 11} };
  34. GLfloat leg[][2] = { {8, 6}, {8, 4}, {9, 3}, {9, 2}, {8, 1}, {8, 0.5}, {9, 0},
  35.   {12, 0}, {10, 1}, {10, 2}, {12, 4}, {11, 6}, {10, 7}, {9, 7} };
  36. GLfloat eye[][2] = { {8.75, 15}, {9, 14.7}, {9.6, 14.7}, {10.1, 15},
  37.   {9.6, 15.25}, {9, 15.25} };
  38. GLfloat lightZeroPosition[] = {10.0, 4.0, 10.0, 1.0};
  39. GLfloat lightZeroColor[] = {0.8, 1.0, 0.8, 1.0}; /* green-tinted */
  40. GLfloat lightOnePosition[] = {-1.0, -2.0, 1.0, 0.0};
  41. GLfloat lightOneColor[] = {0.6, 0.3, 0.2, 1.0}; /* red-tinted */
  42. GLfloat skinColor[] = {0.1, 1.0, 0.1, 1.0}, eyeColor[] = {1.0, 0.2, 0.2, 1.0};
  43. /* *INDENT-ON* */
  44.  
  45. void
  46. extrudeSolidFromPolygon(GLfloat data[][2], unsigned int dataSize,
  47.   GLdouble thickness, GLuint side, GLuint edge, GLuint whole)
  48. {
  49.   static GLUtriangulatorObj *tobj = NULL;
  50.   GLdouble vertex[3], dx, dy, len;
  51.   int i;
  52.   int count = dataSize / (2 * sizeof(GLfloat));
  53.  
  54.   if (tobj == NULL) {
  55.     tobj = gluNewTess();  /* create and initialize a GLU
  56.                              polygon * * tesselation object */
  57.     gluTessCallback(tobj, GLU_BEGIN, glBegin);
  58.     gluTessCallback(tobj, GLU_VERTEX, glVertex2fv);  /* semi-tricky 
  59.                                                       */
  60.     gluTessCallback(tobj, GLU_END, glEnd);
  61.   }
  62.   glNewList(side, GL_COMPILE);
  63.   glShadeModel(GL_SMOOTH);  /* smooth minimizes seeing
  64.                                tessellation */
  65.   gluBeginPolygon(tobj);
  66.   for (i = 0; i < count; i++) {
  67.     vertex[0] = data[i][0];
  68.     vertex[1] = data[i][1];
  69.     vertex[2] = 0;
  70.     gluTessVertex(tobj, vertex, data[i]);
  71.   }
  72.   gluEndPolygon(tobj);
  73.   glEndList();
  74.   glNewList(edge, GL_COMPILE);
  75.   glShadeModel(GL_FLAT);  /* flat shade keeps angular hands
  76.                              from being * * "smoothed" */
  77.   glBegin(GL_QUAD_STRIP);
  78.   for (i = 0; i <= count; i++) {
  79.     /* mod function handles closing the edge */
  80.     glVertex3f(data[i % count][0], data[i % count][1], 0.0);
  81.     glVertex3f(data[i % count][0], data[i % count][1], thickness);
  82.     /* Calculate a unit normal by dividing by Euclidean
  83.        distance. We * could be lazy and use
  84.        glEnable(GL_NORMALIZE) so we could pass in * arbitrary
  85.        normals for a very slight performance hit. */
  86.     dx = data[(i + 1) % count][1] - data[i % count][1];
  87.     dy = data[i % count][0] - data[(i + 1) % count][0];
  88.     len = sqrt(dx * dx + dy * dy);
  89.     glNormal3f(dx / len, dy / len, 0.0);
  90.   }
  91.   glEnd();
  92.   glEndList();
  93.   glNewList(whole, GL_COMPILE);
  94.   glFrontFace(GL_CW);
  95.   glCallList(edge);
  96.   glNormal3f(0.0, 0.0, -1.0);  /* constant normal for side */
  97.   glCallList(side);
  98.   glPushMatrix();
  99.   glTranslatef(0.0, 0.0, thickness);
  100.   glFrontFace(GL_CCW);
  101.   glNormal3f(0.0, 0.0, 1.0);  /* opposite normal for other side 
  102.                                */
  103.   glCallList(side);
  104.   glPopMatrix();
  105.   glEndList();
  106. }
  107.  
  108. void
  109. makeDinosaur(void)
  110. {
  111.   GLfloat bodyWidth = 3.0;
  112.  
  113.   extrudeSolidFromPolygon(body, sizeof(body), bodyWidth,
  114.     BODY_SIDE, BODY_EDGE, BODY_WHOLE);
  115.   extrudeSolidFromPolygon(arm, sizeof(arm), bodyWidth / 4,
  116.     ARM_SIDE, ARM_EDGE, ARM_WHOLE);
  117.   extrudeSolidFromPolygon(leg, sizeof(leg), bodyWidth / 2,
  118.     LEG_SIDE, LEG_EDGE, LEG_WHOLE);
  119.   extrudeSolidFromPolygon(eye, sizeof(eye), bodyWidth + 0.2,
  120.     EYE_SIDE, EYE_EDGE, EYE_WHOLE);
  121.   glNewList(DINOSAUR, GL_COMPILE);
  122.   glMaterialfv(GL_FRONT, GL_DIFFUSE, skinColor);
  123.   glCallList(BODY_WHOLE);
  124.   glPushMatrix();
  125.   glTranslatef(0.0, 0.0, bodyWidth);
  126.   glCallList(ARM_WHOLE);
  127.   glCallList(LEG_WHOLE);
  128.   glTranslatef(0.0, 0.0, -bodyWidth - bodyWidth / 4);
  129.   glCallList(ARM_WHOLE);
  130.   glTranslatef(0.0, 0.0, -bodyWidth / 4);
  131.   glCallList(LEG_WHOLE);
  132.   glTranslatef(0.0, 0.0, bodyWidth / 2 - 0.1);
  133.   glMaterialfv(GL_FRONT, GL_DIFFUSE, eyeColor);
  134.   glCallList(EYE_WHOLE);
  135.   glPopMatrix();
  136.   glEndList();
  137. }
  138.  
  139. void
  140. recalcModelView(void)
  141. {
  142.   glPopMatrix();
  143.   glPushMatrix();
  144.   glRotatef(angle, 0.0, 1.0, 0.0);
  145.   glTranslatef(-8, -8, -bodyWidth / 2);
  146.   newModel = 0;
  147. }
  148.  
  149. void
  150. redraw(void)
  151. {
  152.   if (newModel)
  153.     recalcModelView();
  154.   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  155.   glCallList(DINOSAUR);
  156.   glutSwapBuffers();
  157. }
  158.  
  159. void
  160. mouse(int button, int state, int x, int y)
  161. {
  162.   if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
  163.     moving = 1;
  164.     begin = x;
  165.   }
  166.   if (button == GLUT_LEFT_BUTTON && state == GLUT_UP) {
  167.     moving = 0;
  168.   }
  169. }
  170.  
  171. void
  172. motion(int x, int y)
  173. {
  174.   if (moving) {
  175.     angle = angle + (x - begin);
  176.     begin = x;
  177.     newModel = 1;
  178.     glutPostRedisplay();
  179.   }
  180. }
  181.  
  182. GLboolean lightZeroSwitch = GL_TRUE, lightOneSwitch = GL_TRUE;
  183.  
  184. void
  185. controlLights(int value)
  186. {
  187.   switch (value) {
  188.   case 1:
  189.     lightZeroSwitch = !lightZeroSwitch;
  190.     if (lightZeroSwitch) {
  191.       glEnable(GL_LIGHT0);
  192.     } else {
  193.       glDisable(GL_LIGHT0);
  194.     }
  195.     break;
  196.   case 2:
  197.     lightOneSwitch = !lightOneSwitch;
  198.     if (lightOneSwitch) {
  199.       glEnable(GL_LIGHT1);
  200.     } else {
  201.       glDisable(GL_LIGHT1);
  202.     }
  203.     break;
  204.   }
  205.   glutPostRedisplay();
  206. }
  207.  
  208. int
  209. main(int argc, char **argv)
  210. {
  211.   glutInit(&argc, argv);
  212.   glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  213.   glutCreateWindow("glutdino");
  214.   glutDisplayFunc(redraw);
  215.   glutMouseFunc(mouse);
  216.   glutMotionFunc(motion);
  217.   glutCreateMenu(controlLights);
  218.   glutAddMenuEntry("Toggle right light", 1);
  219.   glutAddMenuEntry("Toggle left light", 2);
  220.   glutAttachMenu(GLUT_RIGHT_BUTTON);
  221.   makeDinosaur();
  222.   glEnable(GL_CULL_FACE);
  223.   glEnable(GL_DEPTH_TEST);
  224.   glEnable(GL_LIGHTING);
  225.   glMatrixMode(GL_PROJECTION);
  226.   gluPerspective( /* field of view in degree */ 40.0,  /* aspect 
  227.                                                           ratio 
  228.                                                         */ 1.0,
  229.     /* Z near */ 1.0, /* Z far */ 40.0);
  230.   glMatrixMode(GL_MODELVIEW);
  231.   gluLookAt(0.0, 0.0, 30.0,  /* eye is at (0,0,30) */
  232.     0.0, 0.0, 0.0,      /* center is at (0,0,0) */
  233.     0.0, 1.0, 0.);      /* up is in postivie Y direction */
  234.   glPushMatrix();       /* dummy push so we can pop on model
  235.                            recalc */
  236.   glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1);
  237.   glLightfv(GL_LIGHT0, GL_POSITION, lightZeroPosition);
  238.   glLightfv(GL_LIGHT0, GL_DIFFUSE, lightZeroColor);
  239.   glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 0.1);
  240.   glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.05);
  241.   glLightfv(GL_LIGHT1, GL_POSITION, lightOnePosition);
  242.   glLightfv(GL_LIGHT1, GL_DIFFUSE, lightOneColor);
  243.   glEnable(GL_LIGHT0);
  244.   glEnable(GL_LIGHT1);
  245.   glutMainLoop();
  246.   return 0;             /* ANSI C requires main to return int. */
  247. }
  248.